package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class workreservation
 */
@WebServlet("/workreservation")
public class workreservation extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public workreservation() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String stuid = request.getParameter("stuid");
		// 预约流水号生产
		// 获取流水号，并且修改新的流水号
		String select = "select * from reservationserial";
		String serial = null;
		Jdbc se = new Jdbc();
		try {
			se.Jdbc_Q2(select);
			se.rs.next();
			serial = se.rs.getString("serialnum");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int serialInt = Integer.parseInt(serial);// 将String转换成INT类型
		DecimalFormat myformat = new DecimalFormat();// 设置流水号编码格式
		myformat.applyPattern("000000");
		String res = (String) myformat.format(serialInt + 1);// 获取最新的流水号
		// 并且要将最新的流水号写入流水号表格中
		String update = "update reservationserial set serialnum='" + res + "'";

		try {
			Jdbc se1 = new Jdbc(update);

		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		String sname = request.getParameter("chinesename");
		String date = request.getParameter("reservationdate");
		String remarks = request.getParameter("remarks");
		String attend = "未记录";
		// 同时将流水号也更新到表中去
		String insert = "insert into reservation(stuid,chinesename,date,remarks,attend,reid)values('" + stuid + "','"
				+ sname + "','" + date + "','" + remarks + "','" + attend + "','" + res + "')";
		Jdbc j1 = null;

		try {
			j1 = new Jdbc(insert);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
		// 此处添加一个验证
		String check1 = "select*from reservation where stuid='" + stuid + "' ";

		Jdbc j2 = null;
		Jdbc j3 = null;
		try {
			j2 = new Jdbc();
			j3 = new Jdbc();
			j2.Jdbc_Q2(check1);
			j2.rs.last();// 将光标移到最后一行
			j2.rs.getRow();

			// 还有预约记录，那么就将sales 更新为已预约，并且查看最新的一条预约记录是否到访
			String check4 = "update sales set reservation='已预约' where id = '" + stuid + "'";
			j3 = new Jdbc(check4);// 将数据库更新
			j2.rs.first();

			String attend1 = j2.rs.getString("attend");
			if (attend1.equals("") || attend1.equals("未到访") || attend1.equals("未记录")) {
				String check5 = "update sales set attend='未出席' where id = '" + stuid + "'";
				j3 = new Jdbc(check5);
			}

		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
